package cn.com.taurus.controller.major;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
 * @author Zht
 * @remark websocket服务端
 * @createTime 2021年8月2日 - 17点25分
 * @varcion 1.0
 */
@ServerEndpoint("/send/{topic}")
@Component
public class WebSocketServer {
    static Logger logger = Logger.getLogger("WebSocketServer");
    /**
     * 静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
     */
    private static int onlineCount = 0;

    /**
     * concurrent包的线程安全Set，用来存放每个客户端对应的MyWebSocket对象。
     */
    private static ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();

    /**
     * 与某个客户端的连接会话，需要通过它来给客户端发送数据
     */
    private Session session;
    /**
     * 接收频道topic
     */
    private String topic = "";

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("topic") String topic) {
        this.session = session;
        this.topic = topic;
        if (webSocketMap.containsKey(topic)) {
            webSocketMap.remove(topic);
            webSocketMap.put(topic, this);
            //加入set中
        } else {
            webSocketMap.put(topic, this);
            //加入set中
            addOnlineCount();
            //在线数加1
        }

        logger.info("用户连接:" + topic + ",当前在线人数为:" + getOnlineCount());
        try {
            sendMessage("连接成功");
        } catch (IOException e) {
            logger.info("用户:" + topic + ",网络异常!!!!!!");
        }
    }


    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        if (webSocketMap.containsKey(topic)) {
            webSocketMap.remove(topic);
            //从set中删除
            subOnlineCount();
        }
        logger.info("用户退出:" + topic + ",当前在线人数为:" + getOnlineCount());
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        logger.info("用户:" + topic + ",信息:" + message);
        //可以群发消息
        //消息保存到数据库、redis
        if (StringUtils.isNotBlank(message)) {
            try {
                //解析发送的报文
                JSONObject jsonObject = JSON.parseObject(message);
                //追加发送人(防止串改)
                jsonObject.put("from_topic", this.topic);
                String to_topic = jsonObject.getString("to_topic");
                //传送给对应toUserId用户的websocket
                if (StringUtils.isNotBlank(to_topic) && webSocketMap.containsKey(to_topic)) {
                    webSocketMap.get(to_topic).sendMessage(jsonObject.toJSONString());
                } else {
                    logger.info("请求的to_topic:" + to_topic + "不在该服务器上");
                    //否则不在这个服务器上，发送到mysql或者redis
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        logger.info("用户错误:" + this.topic + ",原因:" + error.getMessage());
        error.printStackTrace();
    }

    /**
     * 实现服务器主动推送
     */
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }


    /**
     * 发送自定义消息
     */
    public static void sendInfo(String message, @PathParam("topic") String topic) throws IOException {
        logger.info("发送消息到:" + topic + "，信息:" + message);
        if (StringUtils.isNotBlank(topic) && webSocketMap.containsKey(topic)) {
            webSocketMap.get(topic).sendMessage(message);
        } else {
            logger.info("用户" + topic + ",不在线！");
        }
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }
}